function AddFavorite(sURL, sTitle){
    try{
        window.external.addFavorite(sURL, sTitle);
    }catch(e){
        try{
            window.sidebar.addPanel(sTitle, sURL, "");
        }catch (e){
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}
function SetHome(obj,vrl){
    try{
        obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
    }
    catch(e){
        if(window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }catch(e){
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',vrl);
        }
    }
}
jQuery(function ($) {
    $(document).ready(function () {
        $('.navbar-wrapper').stickUp();
        var getTpl = demo.innerHTML;
        var $demo = {
            wrapper: $("#infinite-wrapper"),
            loadingfirst: $("#infinite-wrapper .loading:first"),
            loadingmore: $("#infinite-wrapper .loading:last"),
        };
        var $confg = {
            stop: false,
            margintop: 100,
            url: "/index.php?m=content&c=index&a=ajax_lists&catid=",
            pagesize:35,
            page:2,
            catid:$catid,
        };
        layui.use('laytpl', function () {
            var laytpl = layui.laytpl;
            var window_w = ($(window).width() - 1170) / 2;
            var last_top = $("#right_conner").offset().top + parseFloat($("#right_conner").height());
            $(window).bind("scroll", function () {
                var $li=$("#is_listul").height();
                var $conner=$("#right_conner").height();
                if($li > $conner + 30){
                    showload();
                }
            });
            $(window).resize(function () {
                window_w = ($(window).width() - 1170) / 2;
                last_top = $("#right_conner").offset().top + parseFloat($("#right_conner").height());
                showload();
            })
            function showload() {
                var scrollTop = parseFloat($(window).scrollTop());
                if (scrollTop > last_top) {
                    $("#paddingtop_last").css({
                        "position": "fixed",
                        "top": 0,
                        "right": window_w
                    });
                } else {
                    $("#paddingtop_last").removeAttr("style");
                }
            }
            initialize();
            function initialize() {
                var $new_url = $confg.url + $confg.catid + "&page=1&pagesize=" + $confg.pagesize;
                is_dataajax(1, $new_url)
            }
            $(window).scroll(function () {
                totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop()) + $confg.margintop;
                if ($(document).height() <= totalheight && $confg.stop) {
                    var $new_url = $confg.url + $confg.catid + "&page=" + $confg.page + "&pagesize=" + $confg.pagesize;
                    $demo.loadingmore.fadeIn();
                    is_dataajax(2, $new_url);
                }
            });
            function is_time(value){
                var value=value * 1000;
                var nowDate=Date.parse(new Date());
                var difference=nowDate - value;
                var second=60000; //表示一分钟
                if(difference < second){ //一分钟内
                    return '刚刚';
                }else if(difference < second * 60){
                    var days=Math.floor(difference / second);
                    return days+'分钟前';
                }else if(difference < second * 1440){
                    var days=Math.floor(difference/(second * 60));
                    return days+'小时前';
                }else if(difference < second * 43200){
                    var days=Math.floor(difference/(second * 60 * 24));
                    return days+'天前';
                }else{
                    var d = new Date(value);
                    var year = d.getFullYear();
                    var month = d.getMonth() + 1;
                    var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
                    var hour = d.getHours();
                    var minutes = d.getMinutes();
                    return year+'-'+month+'-'+day+' '+hour+':'+minutes;
                }
            }
            function is_dataajax(type, $new_url) {
                $confg.stop = false;
                var data = '';
                $.ajax({
                    type: "GET",
                    url: $new_url,
                    dataType: "json",
                    success: function (datas) {
                        var data = new Array();
                        if (datas.length === 0) {
                            $demo.loadingmore.fadeOut();
                            return false;
                        }
                        $.each(datas, function () {
                            if (this.thumb !== '') {
                                strs = this.thumb.split("|");
                                this.thumb = strs.slice(0, 4);
                            }
                            this.updatetime=is_time(this.updatetime);
                            this.inputtime=is_time(this.inputtime);
                            if($confg.catid!=''){
                                this.isfrom=false;
                                data.unshift(this);
                            }else{
                                this.isfrom=true;
                                data.push(this);
                            }
                        })
                        laytpl(getTpl).render(data, function (html) {
                            $("#is_listul").append(html);
                            if (type === 1) {
                                $demo.loadingfirst.fadeOut();
                            } else {
                                $confg.page++;
                                $demo.loadingmore.fadeOut();
                            }
                            $confg.stop = true;
                        });
                    },
                    error: function () {
                        $demo.loadingmore.fadeOut();
                        return false;
                    }
                });
            }
        });
        var $slide = {
            buileft: $("#bui-left"),
            slide: $("#is_slide"),
            slidelists: $("#is_slide .slide-list"),
            slidelist: $("#is_slide .slide-list>li"),
            slidetab: $("#is_slide .slide-tab"),
            slidetabli: $("#is_slide .slide-tab>li"),
            index: 0,
            istime: 5000,
        };
        if ($slide.slide.length) {
            var interval = setInterval(function () {
                if ($slide.index === 5) {
                    $slide.index = 0;
                } else {
                    $slide.index++;
                }
                ismouseover($slide.index)
            }, $slide.istime);
            $slide.slidetabli.mouseover(function () {
                var index = $(this).index();
                var $demo = $slide.slidetabli.eq(index);
                if ($demo.hasClass('slide-tab-item-active')) return false;
                ismouseover(index)
            });
            $slide.buileft.hover(function () {
                clearInterval(interval);
            }, function () {
                interval = setInterval(function () {
                    if ($slide.index === 5) {
                        $slide.index = 0;
                    } else {
                        $slide.index++;
                    }
                    ismouseover($slide.index)
                }, $slide.istime);
            });
            function ismouseover(index) {
                $slide.slidetabli.eq(index).addClass('slide-tab-item-active').siblings().removeClass('slide-tab-item-active');
                $slide.slidelist.removeClass('slide-item-active');
                $slide.slidelist.eq(index).addClass('slide-item-active');
                $slide.index = index;
            }
        }
    });
});
